package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.UserMapper;
import cn.scxy.bookcity.pojo.dto.UserAddDTO;
import cn.scxy.bookcity.pojo.dto.UserUpdateDTO;
import cn.scxy.bookcity.pojo.dto.UserUpdatePasswordDTO;
import cn.scxy.bookcity.pojo.entity.User;
import cn.scxy.bookcity.pojo.vo.UserStandardVO;
import cn.scxy.bookcity.service.IUserService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void insert(UserAddDTO userAddDTO) {
        log.debug("开始处理添加用户信息业务,参数:{}",userAddDTO);
        {
            int count = userMapper.countByNickname(userAddDTO.getNickname());
            if (count > 0) {
                String message = "昵称已被占用";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        if(!userAddDTO.getPhone().isEmpty()){
            int count = userMapper.countByPhone(userAddDTO.getPhone());
            if (count == 1) {
                String message = "手机号已被占用";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        if(!userAddDTO.getEmail().isEmpty()){
            int count = userMapper.countByEmail(userAddDTO.getEmail());
            if (count > 0) {
                String message = "邮箱已被占用";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }





        /*if(userAddDTO.getEmail()!=null){
            int count = userMapper.countByEmail(userAddDTO.getEmail());
            if (count > 0) {
                String message = "邮箱已被占用";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }*/
        log.debug("即将向用户表插入数据,参数：{}",userAddDTO);
        User user = new User();
        BeanUtils.copyProperties(userAddDTO,user);
        int i = userMapper.insert(user);
        if(i==0){
            String message = "服务器繁忙,添加失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.debug("添加成功");
    }

    @Override
    public void updateByuId(UserUpdateDTO userUpdateDTO) {
        int i = userMapper.updateByuId(userUpdateDTO);
        if(i==0){
            String message = "服务器繁忙,添加失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.debug("添加成功");
    }

    @Override
    public void update(UserUpdatePasswordDTO userUpdatePasswordDTO) {
        log.debug("开始处理修改密码业务,参数:{}",userUpdatePasswordDTO);
        //查询旧密码是否正确
        {
            UserStandardVO standardVO = userMapper.getStandardById(userUpdatePasswordDTO.getId());
            log.debug("查看旧密码是否正确,用户输入的旧密码:{}",userUpdatePasswordDTO.getOldPassword());
            log.debug("数据库查询到的旧密码:{}",standardVO.getPassword());
            if (!standardVO.getPassword().equals(userUpdatePasswordDTO.getOldPassword())) {
                String message = "原密码错误,请重新输入";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
            }
        }
        //更新密码
        log.debug("开始修改密码,新密码:{}",userUpdatePasswordDTO.getNewPassword());
        int i = userMapper.updatePasswordById(userUpdatePasswordDTO);
        if(i==0){
            String message = "服务器繁忙,修改失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.debug("修改成功");

    }

    @Override
    public String havePhone(Integer id) {
        log.debug("开始处理手机号是否设置,参数:{}",id);
        UserStandardVO standard = userMapper.getStandardById(id);
        String phone = standard.getPhone();
        if(phone.isEmpty()){
            String message = "手机号未设置";
            log.debug(message);
            return message;
        }else {
            String message = "手机号已设置";
            return message;
        }
    }

    @Override
    public String haveEmail(Integer id) {
        log.debug("开始处理邮箱是否设置,参数:{}",id);
        UserStandardVO standard = userMapper.getStandardById(id);
        String email = standard.getEmail();
        log.debug("email:{}",email);
        if(email==null||email.isEmpty()){
            String message = "邮箱未设置";
            log.debug(message);
            return message;
        }else {
            String message = "邮箱已设置";
            return message;
        }
    }

    @Override
    public String security(Integer id) {
        log.debug("开始处理安全等级设置,参数:{}",id);
        UserStandardVO standard = userMapper.getStandardById(id);
        String email = standard.getEmail();
        String phone = standard.getPhone();
        String security = "";
        if(email==null && phone==null){
            security = "低";
            log.debug(security);
            return security;
        }else if(email==null || phone==null){
            security = "中";
            log.debug(security);
            return security;
        }else {
            security = "高";
            log.debug(security);
            return security;
        }
    }

    @Override
    public String selectPhone(Integer id) {
        UserStandardVO standard = userMapper.getStandardById(id);
        return standard.getPhone();
    }

    @Override
    public UserStandardVO getStandardByuId(Integer id) {
        return userMapper.getStandardById(id);
    }
}
